<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

require_once APPPATH . 'controllers/main.php';

/**
 * Контроллер списка всех жанров
 *
 * @package CoreFramework
 * @version 1.0
 * @author Gennadiy Kozlenko
 */
class Genres extends Main {
   
   /**
    * Минимальное количество элементов в колонке
    *
    */
   const MIN_COLUMN_SIZE = 10;
   
   /**
    * Массив найденых результатов
    *
    * @access private
    * @var array
    */
   var $_results = array();
   
   /**
    * Конструктор класса
    *
    * @access public
    * @return Genres
    */
   function Genres() {
      parent::Main();
   } // end Genres
   
   /**
    * Метод по умолчанию
    *
    * @access public
    */
   function index() {
      // Подгружаем результаты
      $this->_load_results();
      
      $left_results = array_slice($this->_results, 0, max(self::MIN_COLUMN_SIZE, ceil(count($this->_results) / 3)));
      $center_results = array_slice($this->_results, count($left_results), max(self::MIN_COLUMN_SIZE, ceil(count($this->_results) / 3)));
      $right_results = array_slice($this->_results, count($left_results) + count($center_results));
      
      $data = array(
         'left_results'   => $left_results,
         'center_results' => $center_results,
         'right_results'  => $right_results
      );
      $this->set_content($this->parser->parse('video/genres.html', $data, true));
      $this->display();
   } // end index
   
   /**
    * Процесс поиска
    *
    * @access private
    */
   function _load_results() {
      $this->benchmark->mark('load_genres_start');
      
      $this->_results = array();
      
      $this->db->select('g.id_genre AS id, g.title, g.slug, COUNT(v.id_video) AS count', false)
         ->from('genres g')
         ->join('video_genres vg', 'vg.id_genre = g.id_genre')
         ->join('videos v', 'vg.id_video = v.id_video')
         ->where('v.status', 'active')
         ->where('v.num_torrents >', 0)
         ->group_by('g.id_genre')
         ->order_by('g.title');
      $query = $this->db->get();
      if (0 < $query->num_rows()) {
         $this->_results = $query->result_array();
      }
      
      $this->benchmark->mark('load_genres_end');
   } // end load_results
   
}
